﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Meqs.Entity;
using Meqs.Logic.Interface;

/// <summary>
/// SettingsViewTemplate 的摘要说明
/// </summary>
public class SettingsViewTemplate : ITemplate, IRequiresSessionState
{
    private static int _itemCount = -1;
    private readonly ListItemType _templateType;
    private IList<EvalItems> _evalItemList;
    private IList<GlobalCauseInfo> _causeList;

    private IEvalSettingsLogic _settingsLogic = Container<IEvalSettingsLogic>.Instance;
    private EvalType _evalType;

    public SettingsViewTemplate(ListItemType type, IList<EvalItems> evalItemList, IList<GlobalCauseInfo> causeList, EvalType evalType)
    {
        _templateType = type;
        _evalItemList = evalItemList;
        _causeList = causeList;
        _evalType = evalType;
    }

    public void InstantiateIn(Control container)
    {
        try
        {
            Literal lc = new Literal();
            switch (_templateType)
            {
                case ListItemType.Header:
                    _itemCount = -1;
                    lc.Text = GeneratorHead();
                    break;
                case ListItemType.Item:
                    lc.Text = GeneratorItem();
                    break;
                case ListItemType.AlternatingItem:
                    lc.Text = GeneratorItem();
                    break;
                case ListItemType.Footer:
                    lc.Text = "</TABLE>";
                    break;
            }
            container.Controls.Add(lc);
            _itemCount += 1;
        }
        catch (Exception ex)
        {
            return;
        }
    }

    /// <summary>
    /// 生成头部文件
    /// </summary>
    /// <returns></returns>
    private string GeneratorHead()
    {
        string header = @"
<table class='useList'>
    <tr>
        <td style='background-color: #D4F1F9'></td>
";
        foreach (EvalItems item in _evalItemList)
        {
            header += "<td style='background-color: #D4F1F9'>" + item.EvalName + "</td>";
        }

        header += "</tr>";
        return header;
    }

    /// <summary>
    /// 生成表格体
    /// </summary>
    /// <returns></returns>
    private string GeneratorItem()
    {
        try
        {
            string body = "<tr>";
            GlobalCauseInfo cause = _causeList[_itemCount];
            body += "<td style='background-color: #D4F1F9'>" + cause.CauseName + "</td>";
            DataTable dt = _settingsLogic.GetSettingsDetail(cause.RID, _evalType, ContainerBasePage.SchoolMark);

            DataRow row = dt.Rows[0];
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                body += "<td>" + row[i].ToString().TrimEnd(',') + "</td>";
            }

            return body;
        }
        catch (Exception)
        {
            return "";
        }
    }
}
